﻿using GEPL.InterfaceLibrary;
using GEPL.StandardModulesLibrary.ViewModels.Common;
using GEPL.StandardModulesLibrary.Views.Common;
using System;
using System.Runtime.Serialization;

namespace GEPL.StandardModulesLibrary.Elements.Common
{
    [Serializable]
    public class ConditionIfElement : IElement
    {
        private readonly ConditionIfViewModel viewModel;

        public ConditionIfElement()
        {
            viewModel = new ConditionIfViewModel();
        }

        public ConditionIfElement(SerializationInfo info, StreamingContext context)
        {
            viewModel = new ConditionIfViewModel()
            {
                EndIf = info.GetBoolean(nameof(viewModel.EndIf)),
                If = info.GetBoolean(nameof(viewModel.If)),
                IfElse = info.GetBoolean(nameof(viewModel.IfElse)),
                Else = info.GetBoolean(nameof(viewModel.Else)),
                Value1 = info.GetString(nameof(viewModel.Value1)),
                Value2 = info.GetString(nameof(viewModel.Value2)),
                SelectedCondition = info.GetString(nameof(viewModel.SelectedCondition))
            };
        }

        #region IElement members

        public ElementType ElementType
        {
            get
            {
                return viewModel.EndIf ? ElementType.EndBlock : viewModel.If ? ElementType.StartBlock : ElementType.MiddleBlock;
            }
        }

        public ProjectType ProjectType
        {
            get { return ProjectType.Any; }
        }

        public string ElementName
        {
            get { return Properties.Resources.Names_Condition; }
        }

        public object Data
        {
            get { return viewModel; }
        }

        public Type UserControlType
        {
            get { return typeof(ConditionIfView); }
        }

        public string GetScript()
        {
            if (viewModel.EndIf)
                return String.Empty;
            if (viewModel.Else)
                return "else:";
            var command = viewModel.If ? "if" : "elif";
            return String.Format("{0} {1} {2} {3}:", command, viewModel.Value1, viewModel.SelectedCondition, viewModel.Value2);
        }

        #endregion IElement members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue(nameof(viewModel.EndIf), viewModel.EndIf);
            info.AddValue(nameof(viewModel.If), viewModel.If);
            info.AddValue(nameof(viewModel.IfElse), viewModel.IfElse);
            info.AddValue(nameof(viewModel.Else), viewModel.Else);
            info.AddValue(nameof(viewModel.Value1), viewModel.Value1, typeof(String));
            info.AddValue(nameof(viewModel.Value2), viewModel.Value2, typeof(String));
            info.AddValue(nameof(viewModel.SelectedCondition), viewModel.SelectedCondition, typeof(String));
        }
    }
}